#include "stm32f4xx_hal.h"
#include "buzzer.h"

extern TIM_HandleTypeDef htim4;

uint32_t beep_cycle, beep_cnt, beep_cnt_loaded;

void buzzer_proc(void){
	if(beep_cycle > 0){
		if(beep_cnt > 0) beep_cnt--; else{
			__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, ((beep_cycle+1) % 2)*125);
			beep_cnt = beep_cnt_loaded;
			beep_cycle--;
		}	
	}
	
}

void beep(uint32_t cycle, uint32_t cnt){
	beep_cycle = 2*cycle; 
	beep_cnt_loaded = cnt;
	beep_cnt = 0;
}

